<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class OxStory 
{
	public $idstory;
	public $title;
	public $desc;

    private $articles_read;
    private $articles_list;
    
    private $score;
    private $timestamp_min;
    private $timestamp_max;
    private $articles_count;
    private $authors_count;
    
    public static function from_id($idstory)
    {
        $new_story = new OxStory();
        $new_story->load($idstory);
        return $new_story;
    }
    
    function __construct()
    {
		$this->clear();
	}
	
	function clear()
	{
        $this->idstory = 0;
        $this->title = '';
        $this->desc = '';

        $this->articles_read = false;
        $this->articles = null;
    }
	
	
	function load($id)
	{
		$this->clear();
        
        $CI =& get_instance();
        
		$story_data = $CI->db->get_where('stories', 'idstory = ' . $id)->result();
		
		$this->idstory = $story_data[0]->idstory;
		$this->title = $story_data[0]->title;
		$this->desc = $story_data[0]->desc;
	}
    
    public function valid()
    {
        if(!$this->articles_read)
        {
            $this->articles();
        }
        return $this->articles_count > 1;
    }
    
    public function articles()
    {
        $CI =& get_instance();
        if(!$this->articles_read)
        {
            $this->articles_list = array();
    		$artcle_id_list = $CI->db->select('idarticle')
                                  ->from('articles')
                                  ->where('idstory', $this->idstory)
                                  ->get()->result();
            
            $this->score = 0;
            
            $this->timestamp_min = null;
            $this->timestamp_max = null;
            
            $this->articles_count = 0;
            
            foreach($artcle_id_list as $article_id)
            {
                $art = OxArticle::from_id($article_id->idarticle);
                
                $this->articles_list[] = $art;
                if($this->score < $art->score) { $this->score = $art->score; }
                
                if($this->timestamp_min == null || $this->timestamp_min > $art->timestamp) 
                        { $this->timestamp_min = $art->timestamp; }
                if($this->timestamp_max == null || $this->timestamp_max < $art->timestamp) 
                        { $this->timestamp_max = $art->timestamp; }
                
                $this->articles_count++;
            }
            
            $count_authors = $CI->db->select('count(distinct iduser) as author_count')
                                  ->from('articles')
                                  ->where('idstory', $this->idstory)
                                  ->get()->result();
            
            $this->authors_count = $count_authors[0]->author_count;   
            
            $this->articles_read = true;
        }
        return $this->articles_list;
    }
    ////////////////////////////////////////////////////////////////////////////////
    // Following are story viewing functions. They must keep all parameters, so to 
    // look like the article showing functions.
    //Note that most CSS properties are article_xxx properties.
    
    

	function info($relevance = false, $show_signature_as_title_by_signature = true,
            $show_score_and_flag_bar = true, $show_timestamp = true, 
            $show_geostamp = true, $show_signature = true)
	{
        if(!$this->articles_read)
        {
            $this->articles();
        }
        
		$text = '<div class="article_preview">';
        if($show_score_and_flag_bar)
        {
            $text .= '<div class="article_preview_stamp">'.$this->score_info().'</div>';
        }
        if($show_signature)
        {
            $text .= '<div class="article_preview_signature">'.$this->writer_info().'</div>';
        }
        
        $text .= '<h1>'.$this->story_title_link().'</h1>';
        if($show_timestamp)
        {
            $text .= '<div class="article_preview_stamping">'.$this->timestamp_info().'</div>';
        }
                    
        $text .= '</div>';
		return $text;		
    }

	function preview($show_tags = false, $show_signature = true)
	{
        if(!$this->articles_read)
        {
            $this->articles();
        }
        
		$text = '<div class="article_preview story_preview">';
        $text .= '<div class="article_preview_stamp">'.$this->score_info().'</div>';
        $text .= '<div class="article_preview_signature">'.$this->writer_info().'</div>';
        /*        
        $text .= '<div class="article_preview_stamping">'.$this->timestamp_info().'</div>';
        */
        $text .= '<h1>'.$this->story_title_link().'</h1>';

        $text .= '<p>'.$this->desc.'</p>';
        foreach($this->articles() as $art)
        {
            /* @var $art OxArticle */
            $text .= $art->info(false, false, true, true, true, true, '', false);
        }

        $text .= '</div>';
		return $text;
    }
    
    function show()
    {
        if(!$this->articles_read)
        {
            $this->articles();
        }
        
		$text = '<div class="article_preview story_preview">';
        $text .= '<div class="article_preview_stamp">'.$this->score_info().'</div>';
        $text .= '<div class="article_preview_signature">'.$this->writer_info().'</div>';
        $text .= '<div class="article_preview_stamping">'.$this->timestamp_info().'</div>';
        
        $text .= '<h1>'.$this->story_title_link().'</h1>';

        $text .= '<p>'.$this->desc.'</p>';
        
        //$text .= '<h2>Articles in the story: </h2>';
        foreach($this->articles() as $art)
        {
            $text .= $art->preview();
        }
        
        $text .= '</div>';
		return $text;       
    }

    function score_info()
	{
		if(isset($this->relevance) && $this->relevance)
		{
			return 'Score: ' . $article->score .
					', target relevance: ' . $this->relevance;
		}
		else
		{
			return 'Score: ' . $this->score;
		}
	}
    
    function writer_info()
    {
        $text = '' . $this->articles_count . ' articles from ' . $this->authors_count . ' authors';
    }
        
    function story_title_link()
    {
		return '<a href="' . site_url('story/' . $this->idstory) . '">' .
					$this->title . '</a>';
    }
    
    function timestamp_info()
    {
        $text = 'Story runs since ' . $this->timestamp_min . ', latest article on ' . $this->timestamp_max;
    }
    
}

/* End of file OxStory.php */